home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 5
/
Aminet 5 - March 1995.iso
/
Aminet
/
mus
/
edit
/
AlgoRhythms.lha
/
AlgoRhythms
/
Source
/
files.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-25
|
15KB
|
487 lines
/* Files.c
Copyright (c) 1990,1991,1992,1993 by Thomas E. Janzen
All Rights Reserved
THIS SOFTWARE IS FURNISHED FREE OF CHARGE FOR STUDY AND USE AND MAY
BE COPIED ONLY FOR PERSONAL USE OR COMPLETELY AS OFFERED WITH NO
CHANGES FOR FREE DISTRIBUTION. NO TITLE TO AND OWNERSHIP OF THE
SOFTWARE IS HEREBY TRANSFERRED. THOMAS E. JANZEN ASSUMES NO
RESPONSIBILITY FOR THE USE OR RELIABILITY OF THIS SOFTWARE.
Thomas E. Janzen
208A Olde Derby Road
Norwood, MA 02062-1761
(617)769-7733
** FACILITY:
**
** AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
** compiled with SAS/C Amiga Compiler 6.50
**
** ABSTRACT:
**
** Files.c handles file reading and writing.
**
** AUTHORS: Thomas E. Janzen
**
** CREATION DATE: 26-MAR-1990
**
** MODIFICATION HISTORY:
** DATE NAME DESCRIPTION
** 4 Jan 92 TEJ last changes for 2.0
** 1 JAN 93 TEJ Changes for V3.0 ; add audio orchestra
**--
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <intuition/intuition.h>
#include <devices/timer.h>
#include "AlgoRhythms.h" /* header for main function */
#include "Window.h" /* header for Window module */
#include "Files.h"
#include "audio.h"
#define BUFLEN 128
static int open_read_file (char *file_name);
static int open_save_file (char *file_name);
FILE *file_ptr; /* pointer to file */
int save_file(char *file_name, /* name of the file */
const struct timeval *total_duration, /* piece's duration */
const int *scale_len, /* num of notes in scale */
const int *scale, /* musical scale */
const int *voices, /* number of voices */
const int *tempo, /* pulses/second */
const FORM_TYPE *form,
const NOTE_EVENT_TYPE *events, /* List of note events */
const NOTE_LEN_TYPE *note_len) /* min & max note length */
/*
** FUNCTIONAL DESCRIPTION:
** Saves a Form File.
**
** RETURN VALUE:
** description: 0 if success; 1 if failed.
** data_type: int
**
** ARGUMENTS:
**
** file_name-
** description: name of the form file to write
** data_type: pointer to char
** access: read only
**
** total_duration-
** description: length of form to write to form file
** data_type: pointer to struct timeval
** access: read only
**
** scale_len-
** description: number of pitches in scale
** data_type: pointer to int
** access: read only
**
** scale-
** description: the scale in MIDI numbers
** data_type: pointer to int
** access: read only
**
** voices-
** description: maximum number of voices to play
** data_type: pointer to int
** access: read only
**
** tempo-
** description: pulse per second
** data_type: pointer to int
** access: read only
**
** form-
** description: period/phase of mean/range
** data_type: pointer to FORM_TYPE
** access: read only
**
**
** note_len-
** description: durations of note in milliseconds
** data_type: NOTE_LEN_TYPE
** access: read only
**
** DESIGN:
**
** ROUTINE save_file
** : sts = open_save_file(file_name)
** : IF sts
** : : return sts
** : ENDIF
** : write duration
** : write min note len
** : write max note len
** : write scale length
** : write scale MIDI pitches
** : write number of voices
** : write tempo
** : write mean period & phase; range period & phase for pitch, duration,
** and dynamic
** : write range period and phase for thickness
** : write channel parameters, one per line
** : close file
** ENDROUTINE
*/
{
auto int sts = 0; /* return status */
register int scale_index, /* Index into the scale */
index; /* general purpose index */
sts = open_save_file(file_name); /* open the file */
if (sts)
{
return sts; /* if file opened, read it in */
}
fprintf(file_ptr, "%4.2f\n", (double)total_duration->tv_secs);
fprintf(file_ptr, "%4.2f\n", ((double)note_len->len_i_min) / 1000.0);
fprintf(file_ptr, "%4.2f\n", ((double)note_len->len_i_max) / 1000.0);
fprintf(file_ptr, "%d\n", *scale_len); /* len of scale */
for (scale_index = 0; scale_index < *scale_len; scale_index++)
{
/*
** read in the whole scale
*/
fprintf(file_ptr, "%d\n", scale[scale_index]);
}
fprintf(file_ptr, "%d\n", *voices); /* read number of voices */
fprintf(file_ptr, "%d\n", *tempo); /* pulses per second */
fprintf(file_ptr, "%4.2f\n", form->frm_s_pitch.prm_d_mean_cycle);
fprintf(file_ptr, "%4.2f\n", form->frm_s_pitch.prm_d_mean_phase);
fprintf(file_ptr, "%4.2f\n", form->frm_s_pitch.prm_d_range_cycle);
fprintf(file_ptr, "%4.2f\n", form->frm_s_pitch.prm_d_range_phase);
/*
** RhythmForm
*/
fprintf(file_ptr, "%4.2f\n", form->frm_s_rhythm.prm_d_mean_cycle);
fprintf(file_ptr, "%4.2f\n", form->frm_s_rhythm.prm_d_mean_phase);
fprintf(file_ptr, "%4.2f\n", form->frm_s_rhythm.prm_d_range_cycle);
fprintf(file_ptr, "%4.2f\n", form->frm_s_rhythm.prm_d_range_phase);
/*
**Dynamics Form
*/
fprintf(file_ptr, "%4.2f\n", form->frm_s_dynamic.prm_d_mean_cycle);
fprintf(file_ptr, "%4.2f\n", form->frm_s_dynamic.prm_d_mean_phase);
fprintf(file_ptr, "%4.2f\n", form->frm_s_dynamic.prm_d_range_cycle);
fprintf(file_ptr, "%4.2f\n", form->frm_s_dynamic.prm_d_range_phase);
/*
** Thickness Form
*/
fprintf(file_ptr, "%4.2f\n", form->frm_s_texture.prm_d_range_cycle);
fprintf(file_ptr, "%4.2f\n", form->frm_s_texture.prm_d_range_phase);
/*
** Read in MAXVOICE Event struct array parameters:
** The lowest pitch, the highest pitch, the MIDI channel, whether
** the voice is walking or random pitch
*/
for (index = 0; index < MAXVOICE; index++)
{
fprintf(file_ptr, "%d %d %d %d %d\n",
events[index].nv_i_low_pitch, events[index].nv_i_high_pitch,
events[index].nv_i_channel, events[index].nv_i_walking,
events[index].nv_i_audio);
}
fprint_orch(file_ptr);
fclose(file_ptr);
return 0;
}
int read_file(char *file_name, /* File Name char array */
struct timeval *total_duration, /* total piece duration */
int *scale_len, /* length of scale */
int *scale, /* musical scale array */
int *voices, /* number of voices (<MAXVOICE) */
int *tempo, /* ptr to pulses per second */
FORM_TYPE *form,
NOTE_EVENT_TYPE *events, /* ptr to array of events */
NOTE_LEN_TYPE *note_len)
/*
** FUNCTIONAL DESCRIPTION:
** Reads a Form File.
**
** RETURN VALUE:
** description: 0 if success; 1 if failed.
** data_type: int
**
** ARGUMENTS:
**
** file_name-
** description: name of the form file to write
** data_type: pointer to char
** access: write only
**
** total_duration-
** description: length of form to write to form file
** data_type: pointer to struct timeval
** access: write only
**
** scale_len-
** description: number of pitches in scale
** data_type: pointer to int
** access: write only
**
** scale-
** description: the scale in MIDI numbers
** data_type: pointer to int
** access: write only
**
** voices-
** description: maximum number of voices to play
** data_type: pointer to int
** access: write only
**
** tempo-
** description: pulse per second
** data_type: pointer to int
** access: write only
**
** form-
** description: period/phase of mean/range for pitch/rhyt/dyn/text
** data_type: pointer to FORM_TYPE
** access: write only
**
** note_len-
** description: durations of a note in milliseconds
** data_type: NOTE_LEN_TYPE
** access: write only
**
** ROUTINE
** : sts = open_read_file(file_name)
** : IF sts
** : : return sts
** : ENDIF
** : read duration
** : read min note len
** : read max note len
** : read scale length
** : read scale MIDI pitches
** : read number of voices
** : read tempo
** : read mean period & phase; range period & phase for pitch, duration,
** and dynamic
** : read range period and phase for thickness
** : read channel parameters, one per line
** : close file
** ENDROUTINE
*/
{
auto int sts = 0; /* sts returned by some functions */
auto int scale_index, /* Index into the scale */
index, /* general purpose counter */
scan_qty,
chair;
static char tempstring[BUFLEN], /* temporary string */
instrument[BUFLEN];
auto char *str_ptr; /* pointer to the temp string */
auto double temp_time,
min_len,
max_len;
sts = open_read_file(file_name);
if (sts)
{
return sts;
}
str_ptr = fgets(tempstring, BUFLEN, file_ptr); /* get duration */
temp_time = strtod(tempstring, (char **)NULL);
total_duration->tv_secs = (int)temp_time;
total_duration->tv_micro = 0L;
str_ptr = fgets(tempstring, BUFLEN, file_ptr); /* min note len */
min_len = strtod(tempstring, (char **)NULL);
note_len->len_i_min = (int)(min_len * 1000.0);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
max_len = strtod(tempstring, (char **)NULL);
note_len->len_i_max = (int)(max_len * 1000.0); /* max note length */
note_len->len_i_dif = note_len->len_i_max - note_len->len_i_min;
scan_qty = fscanf(file_ptr, "%d", scale_len); /* get scale length */
if (scan_qty != 1)
{
*scale_len = 0;
}
for (scale_index = 0; scale_index < (*scale_len); scale_index++)
{
/* read in the whole scale (<120 notes ) */
fscanf(file_ptr, "%d", &scale[scale_index]);
}
scan_qty = fscanf(file_ptr, "%d", voices); /* get num of voices */
if (scan_qty != 1)
{
*voices = 1;
}
scan_qty = fscanf(file_ptr, "%d", tempo); /* get pulse/second */
if (scan_qty != 1)
{
*tempo = 1;
}
str_ptr = fgets(tempstring, BUFLEN, file_ptr); /* read end of line */
/* Get the form structure parameter values */
/*
**Pitch Form
*/
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_pitch.prm_d_mean_cycle = strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_pitch.prm_d_mean_phase = strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_pitch.prm_d_range_cycle
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_pitch.prm_d_range_phase
= strtod(tempstring, (char **)NULL);
/*
** Rhythm Form
*/
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_rhythm.prm_d_mean_cycle
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_rhythm.prm_d_mean_phase
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_rhythm.prm_d_range_cycle
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_rhythm.prm_d_range_phase
= strtod(tempstring, (char **)NULL);
/*
**Dynamics Form
*/
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_dynamic.prm_d_mean_cycle
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_dynamic.prm_d_mean_phase
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_dynamic.prm_d_range_cycle
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_dynamic.prm_d_range_phase
= strtod(tempstring, (char **)NULL);
/*
** Texture Form
*/
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_texture.prm_d_range_cycle
= strtod(tempstring, (char **)NULL);
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
form->frm_s_texture.prm_d_range_phase
= strtod(tempstring, (char **)NULL);
/*
** get per-note values for lowest note, highest note, MIDI
** channel, and whether it's a walking voice
*/
for (index = 0; index < MAXVOICE; index++)
{
str_ptr = fgets(tempstring, BUFLEN, file_ptr);
if (NULL == str_ptr)
{
break;
}
scan_qty = sscanf(str_ptr, "%d %d %d %d %d",
&(events[index].nv_i_low_pitch), &(events[index].nv_i_high_pitch),
&(events[index].nv_i_channel), &(events[index].nv_i_walking),
&(events[index].nv_i_audio));
if (4 == scan_qty)
{
events[index].nv_i_audio = FALSE;
}
}
for (index; index < MAXVOICE; index++)
{
events[index].nv_i_low_pitch = 12;
events[index].nv_i_high_pitch = 97;
events[index].nv_i_walking = FALSE;
events[index].nv_i_channel = 0;
}
while ((str_ptr = fgets(tempstring, BUFLEN, file_ptr)) != NULL)
{
if (strlen(tempstring) != 0)
{
scan_qty = sscanf(str_ptr, "%d %s", &chair, instrument);
if (2 == scan_qty)
{
sts = read_8svx(instrument, chair);
}
}
}
fclose(file_ptr);
return 0;
}
static int open_read_file (char *file_name)
/*
** FUNCTIONAL DESCRIPTION:
** Opens the form file for reading
**
** RETURN VALUE:
** description: 0 if success
** data_type: 1 if failed
**
** ARGUMENTS:
**
** file_name-
** description: name of the form file to open for read
** data_type: pointer to char
** access: read only
**
** DESIGN:
** ROUTINE open_read_file()
** : fopen(file_name)
** : if failed return 1
** : return 0
** ENDROUTINE
*/{
auto char *mode = "r"; /* read mode */
if (NULL == (file_ptr = fopen(file_name, mode)))
{
return 1;
}
return 0;
}
static int open_save_file (char *file_name)
/*
** FUNCTIONAL DESCRIPTION:
** Opens the form file for writing
**
** RETURN VALUE:
** description: 0 if success
** data_type: 1 if failed
**
** ARGUMENTS:
**
** file_name-
** description: name of the form file to open for write
** data_type: pointer to char
** access: read only
**
** ROUTINE open_save_file()
** : fopen(file_name)
** : if failed return 1
** : return 0
** ENDROUTINE
*/
{
auto char *mode = "w"; /* write mode */
if (NULL == (file_ptr = fopen(file_name, mode)))
{
return 1;
}
return 0;
}